package com.hbnu.iostream;

import java.io.*;

/**
 * 需求：使用转换流OutputStreamWriter将数据写入文件
 *
 * @author kittychen
 * @since 2025-10-13 16:49
 */
public class OutputStreamWriterDemo {
    public static void main(String[] args) {
        String path = "C:" + File.separator + "Users" + File.separator + "Administrator"
                + File.separator + "Desktop" + File.separator + "kittychen"
                + File.separator + "class2311" + File.separator + "2311.txt";
        File file = new File(path);

        OutputStreamWriter outputStreamWriter = null;
        try {
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file, true));  // true表示内容追加
            String str = "hello kitty";
            outputStreamWriter.write(str);

            System.out.println("文件写入成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
